package com.lovo.user.mq;

import com.lovo.common.bean.UserInfoBean;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 *  获取直连交换机对应的队列中的消息
 */
@Component
@RabbitListener(queues = {"shop.direct.queue"})
public class DirectReceiver {
    @RabbitHandler
    public void receive(String content,Message message, Channel channel){
        System.out.println("接收到直连交换机的消息："+content);
        try {
            if("dead".equals(content)){
                // 丢到死信队列
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            }else{

                // 确认消息被消费
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RabbitHandler
    public void receive(UserInfoBean content, Message message, Channel channel){
        System.out.println("接收到直连交换机的消息："+content);

        try {
            // 确认消息被消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
